package com.zxp.dubbo.server;

import com.zxp.client.UserService;
import com.zxp.dubbo.service.impl.UserServiceImpl;
import lombok.SneakyThrows;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;

/**
 * @author zxp
 * @date 2021-08-24 9:40
 */
public class DubboServer {

    @SneakyThrows
    public static void main(String[] args) {
        // 配置： application、协议、注册中心、服务
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("zxp-dubbo");
        //applicationConfig.setQosAcceptForeignIp(false);
        //applicationConfig.setQosEnable(false);

        // 协议
        ProtocolConfig protocolConfig=new ProtocolConfig();
        protocolConfig.setName("dubbo");
        protocolConfig.setPort(-1); // 默认是20880, -1为生成随机端口号
        //protocolConfig.setThreads(20);
        protocolConfig.setThreads(20);

        // 直连：RegistryConfig.NO_AVAILABLE  使用multicast广播注册中心暴露服务地址 "multicast://224.1.1.1:1234",虚拟的注册中心，局域网
        //RegistryConfig registryConfig = new RegistryConfig(RegistryConfig.NO_AVAILABLE);
        //RegistryConfig registryConfig = new RegistryConfig("multicast://224.1.1.1:3333?unicast=false");
        RegistryConfig registryConfig = new RegistryConfig("zookeeper://192.168.56.160:2181");

        ServiceConfig serviceConfig = new ServiceConfig();
        serviceConfig.setRegistry(registryConfig);
        serviceConfig.setProtocol(protocolConfig);
        serviceConfig.setApplication(applicationConfig);
        serviceConfig.setInterface(UserService.class);
        serviceConfig.setRef(new UserServiceImpl());

        // 暴露服务
        serviceConfig.export();
        System.out.println("dubbo 成功暴露服务");
        System.in.read();
    }
}
